package com.acme.bankapp.domain.bank;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Artem Kalchenko
 *
 */
public class Client implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String name;
	private Gender gender;
	private List<Account> accounts = new ArrayList<Account>();

	public Client(final String name, final Gender gender) {
		this.name = name;
		this.gender = gender;
	}

	public Client(final String name, final Gender gender, final Account account) {
		this.name = name;
		this.gender = gender;
		this.addAccount(account);
	}

	public String getName() {
		return name;
	}
	
	public Gender getGender() {
		return gender;
	}

	public String getClientSalutation() {
		return gender.getSalutation() + " " + name;
	}

	public Account getAccount() {
		return accounts.get(0);
	}

	public void addAccount(final Account account) {
		account.setClient(this);
		accounts.add(account);
	}

	public List<Account> getAccounts() {
		return Collections.unmodifiableList(accounts);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		StringBuffer buff = new StringBuffer();
		
		buff.append(this.getClientSalutation());
		buff.append(" accounts: ");
		for (Account acc: accounts){
			buff.append(acc.toString());
		}
		return buff.toString();
	}
}
